package com.kevin.blog.service;

import com.kevin.blog.NotFoundException;
import com.kevin.blog.dao.AdminCommentRepository;
import com.kevin.blog.po.Comment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Kevin
 * @Date 2021-04-29 10:36:38
 */
@Service
public class AdminCommentServiceImpl implements AdminCommentService {

    @Autowired
    private AdminCommentRepository adminCommentRepository;

    @Transactional
    @Override
    public Comment saveComment(Comment Comment) {
        return adminCommentRepository.save(Comment);
    }

    @Override
    public Comment getComment(Long id) {
        return adminCommentRepository.getOne(id);
    }

    @Override
    public Page<Comment> listComment(Pageable pageable) {
        return adminCommentRepository.findAll(pageable);
    }

    @Override
    public List<Comment> listComment() {
        return adminCommentRepository.findAll();
    }

    @Transactional
    @Override
    public Comment updateComment(Long id, Comment Comment) {
        Comment c = adminCommentRepository.getOne(id);
        if (c == null) {
            throw new NotFoundException("不存在该评论");
        }
        //使用Spring的BeanUtils赋值到Comment
        BeanUtils.copyProperties(Comment, c);
        return adminCommentRepository.save(c);
    }

    @Transactional
    @Override
    public void deleteComment(Long id) {
        adminCommentRepository.deleteById(id);
    }
}
